package com.xoa.service.party;


import com.xoa.dao.party.EsCommitteeMapper;
import com.xoa.model.party.EsCommittee;
import com.xoa.model.users.Users;
import com.xoa.util.DateFormat;
import com.xoa.util.ExcelUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.log.FileUtils;
import com.xoa.util.common.newwrapper.BaseWrapper;
import com.xoa.util.common.session.SessionUtils;
import com.xoa.util.page.PageParams;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by gsb on 2018/1/11.
 */
@Service
public class EsCommitteeService {

    @Resource
    private EsCommitteeMapper esCommitteeMapper;

    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-11 下午14:42:51
     * 类介绍  :  新建
     * 构造参数:
     *
     */
    public ToJson<EsCommittee> insertSelective(EsCommittee esCommittee, HttpServletRequest request, String gridLocation){
        ToJson<EsCommittee> json = new ToJson<>();
        try {
            Users user= SessionUtils.getSessionInfo(request.getSession(), Users.class, new Users());
            if(gridLocation!=null){
                esCommittee.setC1(gridLocation);
            }
            esCommittee.setCreateUser(user.getUserId());
            esCommittee.setCreateTime(DateFormat.getDatestr(new Date()));
            esCommitteeMapper.insertSelective(esCommittee);
            json.setMsg("新建成功");
            json.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }
    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-11 下午15:22:51
     * 类介绍  :  删除
     * 构造参数:
     *
     */
    public ToJson<EsCommittee> deleteByPrimaryKey(Integer cid){
        ToJson<EsCommittee> json = new ToJson<>();
        try {
            esCommitteeMapper.deleteByPrimaryKey(cid);
            json.setMsg("成功");
            json.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }
    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-11 下午15:22:51
     * 类介绍  :  修改
     * 构造参数:
     *
     */
    public ToJson<EsCommittee> updateByPrimaryKeySelective(EsCommittee esCommittee, String gridLocation){
        ToJson<EsCommittee> json = new ToJson<>();
        try {
            if(gridLocation!=null){
                esCommittee.setC1(gridLocation);
            }
            esCommitteeMapper.updateByPrimaryKeySelective(esCommittee);
            json.setMsg("成功");
            json.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }
    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-11 下午15:22:51
     * 类介绍  :  详情
     * 构造参数:
     *
     */

    public ToJson<EsCommittee> selectById(Integer cid){
        ToJson<EsCommittee> json = new ToJson<>();
        try {
            EsCommittee esCommittee = esCommitteeMapper.selectById(cid);
            json.setObject(esCommittee);
            json.setMsg("成功");
            json.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }
    /**
     *
     * 创建作者:   季佳伟
     * 创建日期:   2018-1-11 下午15:22:51
     * 类介绍  :  列表
     * 构造参数:
     *
     */
    public ToJson<EsCommittee> queryList(EsCommittee esCommittee, Integer page, Integer pageSize,
                                         boolean useFlag, HttpServletRequest request, HttpServletResponse response, String export){
        ToJson<EsCommittee> json = new ToJson<>();
        try {
            PageParams pageParams=new PageParams();
            if(page!=null){
                page=pageSize*(page-1);
            }
            Map<String, Object> map = new HashMap<String, Object>();
            if(useFlag){
                map.put("page", page);
                map.put("pageSize", pageSize);
            }
            map.put("esCommittee",esCommittee);
            if(export==null){
                export="0";
            }
            List<EsCommittee> esCommitteeList = esCommitteeMapper.selectList(map);
            json.setObj(esCommitteeList);
            json.setTotleNum(esCommitteeMapper.selectCount(esCommittee));
            json.setMsg("成功");
            json.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }



    /**
    * @Author 程叶同
    * @Description 导出
    * @Date 15:10 2018/3/6
    */

    public BaseWrapper outPutFile(HttpServletRequest request, HttpServletResponse response){
        BaseWrapper baseWrapper=new BaseWrapper();
        Map<String,Object> map=new HashMap<String,Object>();
        List<EsCommittee> esCommitteeList = esCommitteeMapper.selectList(map);
        /*if(ids!=null&&ids!=""){
            String[] id=ids.split(",");
            Map<String,Object> map=new HashMap<String,Object>();
            map.put("id",id);
            illegalrentgroups=illegalrentgroupMapper.selectList(map);
        }*/

        try {

            HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("社区居委会表", 9);
            String[] secondTitles = {"具体位置","负责人","主管部门","联系方式"};
            HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbook1, secondTitles);

            // String[] beanProperty = {user.getDep().getDeptName(),"userName","userPrivName", "roleAuxiliaryName","online","sex","online","telNoDept","telNoDept","departmentPhone","email"};
            String[] beanProperty = {"location","responsible","directorDept","contact"};

            HSSFWorkbook workbook = ExcelUtil.exportExcelData(workbook2, esCommitteeList, beanProperty);
            ServletOutputStream out = response.getOutputStream();

            String filename = "社区居委会表.xls";
            filename = FileUtils.encodeDownloadFilename(filename,
                    request.getHeader("user-agent"));
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-disposition",
                    "attachment;filename=" + filename);
            workbook.write(out);
            out.flush();
            out.close();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        }

        return  baseWrapper;
    }


    /**
     *
     * 创建作者:
     * 创建日期:   2018-1-11 下午15:22:51
     * 类介绍  :  列表新
     * 构造参数:
     *
     */
    public ToJson<EsCommittee> queryListNew(EsCommittee esCommittee, Integer page, Integer pageSize,
                                            boolean useFlag, HttpServletRequest request, HttpServletResponse response, String export){
        ToJson<EsCommittee> json = new ToJson<>();
        try {
            PageParams pageParams=new PageParams();
            if(page!=null){
                page=pageSize*(page-1);
            }
            Map<String, Object> map = new HashMap<String, Object>();
            if(useFlag){
                map.put("page", page);
                map.put("pageSize", pageSize);
            }
            map.put("esCommittee",esCommittee);
            if(export==null){
                export="0";
            }
            List<EsCommittee> esCommitteeList = esCommitteeMapper.selectListNew(map);
            json.setObj(esCommitteeList);
            json.setTotleNum(esCommitteeMapper.selectCount(esCommittee));
            json.setMsg("成功");
            json.setFlag(0);
        }catch (Exception e){
            e.printStackTrace();
            json.setFlag(1);
            json.setMsg(e.getMessage());
        }
        return json;
    }
}
